using Commons.Models.Settings.Dtos.TradeValues;
using Commons.Models.Settings.Mappers.Functions;
using Commons.Models.Settings.Mappers.Locks;
using Commons.Models.Stats;
using Commons.Models.TradeValues;
using System;

namespace Commons.Models.Settings.Mappers.TradeValues
{
	public class UserStatTradeValueMapper : IMapper<TradeValueDto, ITradeValue>
	{
		public ITradeValue Map (TradeValueDto config)
		{
			UserStatTradeValueDto dto = (UserStatTradeValueDto)config;

			MultipleTradeValue tradeValue = new MultipleTradeValue();

			if (dto.Credits.HasValue)
			{
				tradeValue.AddValue(new UserStatTradeValue(StatUnit.Credits, dto.Credits.Value));
			}

			if (dto.Score.HasValue)
			{
				tradeValue.AddValue(new UserStatTradeValue(StatUnit.Score, dto.Score.Value));
			}

			if (dto.Xp.HasValue)
			{
				tradeValue.AddValue(new UserStatTradeValue(StatUnit.Xp, dto.Xp.Value));
			}

            if (dto.Lifes.HasValue)
            {
                tradeValue.AddValue(new UserStatTradeValue(StatUnit.Lifes, dto.Lifes.Value));
            }

            if (dto.Score.HasValue)
            {
                tradeValue.AddValue(new UserStatTradeValue(StatUnit.Score, dto.Score.Value));
            }

			tradeValue.Lock = new LockMapper().Map(dto.Lock);

			if (tradeValue.Values.Count > 1)
			{
				if (dto.Function != null)
				{
					throw new Exception("Multi stats trade values can't use function");
				}

				return tradeValue;
			}
			else if (tradeValue.Values.Count == 1)
			{
				UserStatTradeValue statTradeValue = (UserStatTradeValue)tradeValue.Values[0];
				if (dto.Function != null)
				{
					statTradeValue.Function = new FunctionMapper().Map(dto.Function);
				}
				return statTradeValue;
			}
			else
			{
				throw new Exception("Stats trade value can't be empty");
			}
		}
	}
}